value of the variable in the external function.
Closures for use:Now that the concept of closure is clear, let's look at the purpose of closures. In fact, we can do a lot of things by using closures. For example, simulate object-oriented code style, more elegant, more concise expression of code, in some ways to improve the efficiency of code execution.
Cache:To take another example, imagine that we have a function object that is time-consuming to process, each call takes a long time, so we ne
follows:Var CachedSearchBox = (function (){Var cache = {},Count = [];Return {AttachSearchBox: function (dsid ){If (dsid in cache) {// if the result is in the cacheReturn cache [dsid]; // directly return the objects in the cache}Var fsb = new uikit. webctrl. SearchBox (dsid); // createCache [
look at the purpose of closure. In fact, we can do a lot of things by using closures. For example, it simulates the object-oriented code style. It is more elegant and concise to express the Code. In some aspects, it improves the code execution efficiency.Cache:Let's take a look at an example. Suppose we have a function object that takes a long time to process. Every call will take a long time, so we need to store the calculated value, when this function is called, it is first searched in the ca
var circle={"PI": 3.14159,"Area": function (r) {return this. PI * R * r;}};Alert (Circle.area (1.0));Anonymous self-executing functionsvar data= {Table: [],Tree: {}};(function (DM) {for (var i = 0; i var row = Dm.table.rows[i];for (var j = 0; j Drawcell (i, j);}}}) (data);The result cache [Closure is exactly what you can do because it does not release external references, so the values inside the function can be preserved. ]var Cachedsearchbox = (function () {var cache = {},Count = [];return {At
follows:
Var CachedSearchBox = (function (){Var cache = {},Count = [];Return {AttachSearchBox: function (dsid ){If (dsid in cache) {// if the result is in the cacheReturn cache [dsid]; // directly return the objects in the cache}Var fsb = document. getElementById (dsid); // createCache [
Cachedsearchbox = (function () {
var cache = {},
Count = [];
return {
Attachsearchbox:function (DSID) {
if (Dsid in cache) {//If the result is in the cache
Return cache[dsid];//directly to the object in the cache
}
var FSB = new Uikit.webctrl.SearchBox (DSID);/new
CACHE[D
the value to find. Closures can do this because it does not release external references, so values inside the function can be preserved.var Cachedsearchbox = (function(){var cache ={}, Count =[];Return{attachsearchbox:function(DSID) {if (dsidIn cache) {//If the result is in the cachereturn CACHE[DSID];//Directly returns objects in the cache}var FSB = new Uikit.webctrl.SearchBox (
{attachsearchbox:function(DSID) {if (dsidIn cache) {//If the result is in the cachereturn CACHE[DSID];//Directly returns objects in the cache}var FSB = new Uikit.webctrl.SearchBox (DSID); // new CACHE[DSID] = FSB; // update cache if ( Count.length > {// the size of the positive cache delete Cache[count.shift ()];} retu
= (function(){ varCache ={}, Count= []; return{attachsearchbox:function(DSID) {if(DsidinchCache) {//If the result is in the cache returnCACHE[DSID];//directly returns objects in the cache } varFSB =NewUikit.webctrl.SearchBox (DSID);//NewCACHE[DSID] = FSB;//Update Cache
calculate, then update the cache and return the value, if found, Return directly to the value you are looking for. Closures can do this because it does not release external references, so values inside the function can be preserved.varCachedsearchbox = (function(){ varCache ={}, Count= []; return{attachsearchbox:function(DSID) {if(DsidinchCache) {//If the result is in the cache returnCACHE[DSID
the development, imagine that we have a process is a time-consuming function object, each call will take a long time,Then we need to store the calculated value, when the function is called, first in the cache to find, if not found, then calculate, and then update the cache and return the value, if found, directly return the value to find. Closures can do this because it does not release external references, so values inside the function can be preserved.varCachedsearchbox = (function(){
= (function(){ varCache ={}, Count= []; return{attachsearchbox:function(DSID) {if(DsidinchCache) {//If the result is in the cache returnCACHE[DSID];//directly returns objects in the cache } varFSB =NewUikit.webctrl.SearchBox (DSID);//NewCACHE[DSID] = FSB;//Update Cache
references, so values inside the function can be preserved.varCachedsearchbox = (function(){ varCache ={}, Count= []; return{attachsearchbox:function(DSID) {if(DsidinchCache) {//If the result is in the cache returnCACHE[DSID];//directly returns objects in the cache } varFSB =NewUikit.webctrl.SearchBox (DSID);//NewCACHE
{attachsearchbox:function(DSID) {if(DsidinchCache) {//If the result is in the cache returnCACHE[DSID];//directly returns objects in the cache } varFSB =NewUikit.webctrl.SearchBox (DSID);//NewCACHE[DSID] = FSB;//Update Cache if(Count.length > 100) {//the size of the positiv
) { Drawcell (i, j);}} ) (data); We create an anonymous function and execute it immediately, since the external cannot reference its internal variables, so the resources are freed immediately after the function is executed, and the key is not to pollute the global object.2. Result CacheWe will encounter a lot of situations in the development, imagine that we have a process is a time-consuming function object, each call will take a long time,Then we need to store the ca
function object, each call will take a long time,Then we need to store the calculated value, when the function is called, first in the cache to find, if not found, then calculate, and then update the cache and return the value, if found, directly return the value to find. Closures can do this because it does not release external references, so values inside the function can be preserved.varCachedsearchbox = (function(){ varCache ={}, Count= []; return{attachsearchbox:function(
) { Drawcell (i, j);}} ) (data); We create an anonymous function and execute it immediately, since the external cannot reference its internal variables, so the resources are freed immediately after the function is executed, and the key is not to pollute the global object .2. Result CacheWe will encounter a lot of situations in the development, imagine that we have a process is a time-consuming function object, each call will take a long time,Then we need to store
does not release external references, so values inside the function can be preserved. var Cachedsearchbox = (function () {var cache = {}, Count = []; return {attachsearchbox:function (DSID) {if (Dsid in cache) {//If the result is in the cache Return cache[dsid];//directly returns the object in the cache} var FSB
{ attachsearchbox:function (dsid) { if (Dsid in cache) {//If the result is returned in the cache cache[dsid];//returns the object in the cache directly } var FSB = new Uikit.webctrl.SearchBox (DSID);//New Cache[
are freed immediately after the function is executed, and the key is not to pollute the global object.2. Result CacheWe will encounter a lot of situations in the development, imagine that we have a process is a time-consuming function object, each call will take a long time,Then we need to store the calculated value, when the function is called, first in the cache to find, if not found, then calculate, and then update the cache and return the value, if found, directly return the value to find.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.